package cn.myframe.redis.msg;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author: ynz
 * @Date: 2018/12/8/008 20:52
 */
@Component
public class HelloListener {
    @Autowired
    HelloReceiver helloReceiver;
    @Resource
    RedisConnectionFactory connectionFactory;

    /**
     * 监听方法
     * @param adapter
     * @return
     */
    @Bean("containerHello")
    RedisMessageListenerContainer containe(@Qualifier("printAdapter")  MessageListenerAdapter adapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(adapter, new PatternTopic("helloRedis"));
        return container;
    }

    /**
     * 消息处理方法的适配器
     * @return
     */
    @Bean(name="printAdapter")
    MessageListenerAdapter adapter() {
        //回调数据处理方法
        return new MessageListenerAdapter(helloReceiver, "print");
    }
}
